package com.yanqu.road.server.cmd.activity.xiuxian.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.xiuxian.config.XiuXianConfig;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.recharge.RechargeBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XiuXianProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.xiuxian.XiuXianMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.Date;

@Cmd(code = GameProtocol.S_XIU_XIAN_GET_SERVER_SCORE_FROM_CROSS, desc = "向区服获取区服评分")
public class XiuXianGetServerScoreFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XiuXianProto.XiuXianGetServerScoreToGameServerMsg reqMsg = XiuXianProto.XiuXianGetServerScoreToGameServerMsg.parseFrom(packet.getBytes());
        ActivityInfo activityInfo = XiuXianMgr.getActivityInfo();
        if (activityInfo == null) {
            return;
        }
        XiuXianConfig config = XiuXianMgr.getConfig();
        if (config == null) {
            return;
        }
        if (reqMsg.getActivityId() != activityInfo.getActivityId()) {
            return;
        }
        log.info("【凡人修仙】区服数据采集开始。。。。。。。。。。。");
        XiuXianProto.XiuXianGetServerScoreToCrossMsg.Builder builder = XiuXianProto.XiuXianGetServerScoreToCrossMsg.newBuilder();
        builder.setActivityId(reqMsg.getActivityId());
        // 获取评分
        String[] split = config.getXIUXIAN_SECT_SCORE_PRM().split("\\|");
        String p1 = split[0];
        String p2 = split[1];
        // 获取区服的总赚钱
        BigInteger totalAbility = BigInteger.ZERO;
        for (UserInfo userInfo : UserMgr.getUserInfoList()) {
            if (userInfo == null) {
                continue;
            }
            if (DateHelper.calc2DateTDOADays(new Date(userInfo.getLastLoginTime() * 1000), new Date()) <= 7) {
                totalAbility = totalAbility.add(userInfo.getTotalAbility());
            }
        }
        log.info("【凡人修仙】区服数据采集,区服:{},近七天门客总赚钱:{}", GameServer.getInstance().getServerId(), totalAbility.longValue());
        // 获取区服充值总额
        long allRechargeAmount = RechargeBussiness.getAllRechargeAmount(new Date(reqMsg.getBeginTime()), new Date()) / 100;
        log.info("【凡人修仙】区服数据采集,区服:{},近七天充值总额:{}", GameServer.getInstance().getServerId(), allRechargeAmount);
        long serverScore = totalAbility.divide(new BigInteger(p1.split("=")[0])).multiply(new BigInteger(p1.split("=")[1])).add(
                new BigInteger(String.valueOf(allRechargeAmount)).divide(new BigInteger(p2.split("=")[0])).multiply(new BigInteger(p2.split("=")[1]))
        ).longValue();
        builder.setServerScore(serverScore);
        log.info("【凡人修仙】区服数据采集结束,开始上报跨服。。。。。。。。。。。");
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_CROSS_XIU_XIAN_GET_SERVER_SCORE, builder));
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
